package com.moyuxi.sound_of_the_forest.sql

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.moyuxi.sound_of_the_forest.entity.LocalMusicBean

@Database(
    entities = [LocalMusicBean::class],//指定表
    version = 1,//版本
    exportSchema = false//版本升级是否记录到本地，暂时为false
)
abstract class MusicListDataBase : RoomDatabase() {
    companion object {

        private var INSTANCE: MusicListDataBase? = null

        fun init(context: Context) {
            if (INSTANCE == null) synchronized(MusicListDataBase::class.java) {
                if (INSTANCE == null) create(context)
            }
        }

        private fun create(context: Context) {
            INSTANCE = Room.databaseBuilder(context, MusicListDataBase::class.java, "music_db")
                .allowMainThreadQueries()//可以在主线程执行查询，不建议这么做
                .fallbackToDestructiveMigration()//数据库改变时，强制升级时，不报错
                .build()

        }

        fun getDB(): MusicListDataBase {
            return INSTANCE ?: throw NullPointerException("UserDataBase 未初始化")
        }

    }


    //增加抽象方法，Room框架会自动实现这个方法
    abstract fun getMusicListDao(): MusicListDao
}